package com.sumansoul.multiplerecycleview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter{
    private Context mContext;
    private List<News> mNewsList;
    private News mNews;
    public static final int TYPE_TEXT = 0;
    public static final int TYPE_VIDEO = 1;
    public static final int TYPE_PIC = 2;
    public static final int TYPE_PIC2 = 3;
    public NewsAdapter( Context mContext, List<News> mNewsList) {
        super();
        this.mContext = mContext;
        this.mNewsList = mNewsList;
    }

    @Override
    public int getItemCount() {
        return mNewsList.size();
    }

    /**
     * 不同条目
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        mNews = mNewsList.get(position);
        int newsType = mNews.getNewsType();
        if(newsType == TYPE_TEXT){
            return TYPE_TEXT;
        }else if (newsType == TYPE_VIDEO){
            return TYPE_VIDEO;
        }else if (newsType == TYPE_PIC){
            return TYPE_PIC;
        }else if (newsType == TYPE_PIC2){
            return TYPE_PIC2;
        }else {
            return TYPE_TEXT;
        }
    }

    /**
     * 分不同布局加载
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if(viewType == TYPE_TEXT){
            view =View.inflate(parent.getContext(),R.layout.item_news_content,null);
            return new TextViewHolder(view);
        }else if(viewType == TYPE_VIDEO){
            view =View.inflate(parent.getContext(),R.layout.item_video_content,null);
            return new VideoViewHolder(view);
        }else if(viewType == TYPE_PIC){
            view =View.inflate(parent.getContext(),R.layout.item_news_pic,null);
            return new VideoViewHolder(view);
        }else if(viewType == TYPE_PIC2){
            view =View.inflate(parent.getContext(),R.layout.item_news_pic2,null);
            return new PicViewHolder(view);
        }
        return null;
    }

    /**
     * 绑定不同布局
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof TextViewHolder){
            TextViewHolder textViewHolder = (TextViewHolder) holder;
        }else if (holder instanceof VideoViewHolder){
            VideoViewHolder videoHolder = (VideoViewHolder) holder;
        }else if (holder instanceof PicViewHolder){
            PicViewHolder picViewHolder = (PicViewHolder) holder;
        }else {
            ContentViewHolder contentViewHolder = (ContentViewHolder) holder;
        }
    }
    final class TextViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView iv_pic1;
        ImageView iv_pic2;
        ImageView iv_pic3;

        public TextViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            iv_pic1 = (ImageView) itemView.findViewById(R.id.iv_pic1);
            iv_pic2 = (ImageView) itemView.findViewById(R.id.iv_pic2);
            iv_pic3 = (ImageView) itemView.findViewById(R.id.iv_pic3);
        }
    }

    final class VideoViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView iv_pic1;

        public VideoViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            iv_pic1 = (ImageView) itemView.findViewById(R.id.iv_pic1);
        }
    }

    final class PicViewHolder extends RecyclerView.ViewHolder{

        ImageView iv_pic1;
        ImageView iv_pic2;

        public PicViewHolder(View itemView) {
            super(itemView);
            iv_pic1 = (ImageView) itemView.findViewById(R.id.iv_pic1);
            iv_pic2 = (ImageView) itemView.findViewById(R.id.iv_pic2);
        }
    }

    final class ContentViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        TextView tv_content;

        public ContentViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
        }
    }
}
